home *** CD-ROM | disk | FTP | other *** search
Text File | 1995-01-29 | 10.5 KB | 425 lines | [TEXT/MPCC] |
-
- //============================================================================
- //----------------------------------------------------------------------------
- // Utilities.c
- //----------------------------------------------------------------------------
- //============================================================================
-
-
- #include "Externs.h"
-
-
- #define kActive 0
- #define kInactive 255
-
-
- GDHandle thisGDevice;
- long tickNext;
-
-
- //============================================================== Functions
- //-------------------------------------------------------------- RandomInt
-
- short RandomInt (short range)
- {
- register long rawResult;
-
- rawResult = Random();
- if (rawResult < 0L)
- rawResult *= -1L;
- rawResult = (rawResult * (long)range) / 32768L;
-
- return ((short)rawResult);
- }
-
- //-------------------------------------------------------------- RedAlert
-
- void RedAlert (StringPtr theStr)
- {
- #define kRedAlertID 128
- short whoCares;
-
- ParamText(theStr, "\p", "\p", "\p");
- whoCares = Alert(kRedAlertID, 0L);
- ExitToShell();
- }
-
- //-------------------------------------------------------------- FindOurDevice
-
- void FindOurDevice (void)
- {
- thisGDevice = GetMainDevice();
- if (thisGDevice == 0L)
- RedAlert("\pCouldn't Find Our Device");
- }
-
- //-------------------------------------------------------------- LoadGraphic
-
- void LoadGraphic (short resID)
- {
- Rect bounds;
- PicHandle thePicture;
-
- thePicture = GetPicture(resID);
- if (thePicture == 0L)
- RedAlert("\pA Graphic Couldn't Be Loaded");
-
- HLock((Handle)thePicture);
- bounds = (*thePicture)->picFrame;
- HUnlock((Handle)thePicture);
- OffsetRect(&bounds, -bounds.left, -bounds.top);
- DrawPicture(thePicture, &bounds);
-
- ReleaseResource((Handle)thePicture);
- }
-
- //-------------------------------------------------------------- CreateOffScreenPixMap
-
- void CreateOffScreenPixMap (Rect *theRect, CGrafPtr *offScreen)
- {
- CTabHandle thisColorTable;
- GDHandle oldDevice;
- CGrafPtr newCGrafPtr;
- Ptr theseBits;
- long sizeOfOff, offRowBytes;
- OSErr theErr;
- short thisDepth;
-
- oldDevice = GetGDevice();
- SetGDevice(thisGDevice);
- newCGrafPtr = 0L;
- newCGrafPtr = (CGrafPtr)NewPtrClear(sizeof(CGrafPort));
- if (newCGrafPtr != 0L)
- {
- OpenCPort(newCGrafPtr);
- thisDepth = (**(*newCGrafPtr).portPixMap).pixelSize;
- offRowBytes = ((((long)thisDepth *
- (long)(theRect->right - theRect->left)) + 15L) >> 4L) << 1L;
- sizeOfOff = (long)(theRect->bottom - theRect->top) * offRowBytes;
- OffsetRect(theRect, -theRect->left, -theRect->top);
- theseBits = NewPtr(sizeOfOff);
- if (theseBits != 0L)
- {
- (**(*newCGrafPtr).portPixMap).baseAddr = theseBits;
- (**(*newCGrafPtr).portPixMap).rowBytes = (short)offRowBytes + 0x8000;
- (**(*newCGrafPtr).portPixMap).bounds = *theRect;
- thisColorTable = (**(**thisGDevice).gdPMap).pmTable;
- theErr = HandToHand((Handle *)&thisColorTable);
- (**(*newCGrafPtr).portPixMap).pmTable = thisColorTable;
- ClipRect(theRect);
- RectRgn(newCGrafPtr->visRgn, theRect);
- ForeColor(blackColor);
- BackColor(whiteColor);
- EraseRect(theRect);
- }
- else
- {
- CloseCPort(newCGrafPtr);
- DisposePtr((Ptr)newCGrafPtr);
- newCGrafPtr = 0L;
- RedAlert("\pCouldn't Allocate Enough Memory");
- }
- }
- else
- RedAlert("\pCouldn't Allocate Enough Memory");
-
- *offScreen = newCGrafPtr;
- SetGDevice(oldDevice);
- }
-
- //-------------------------------------------------------------- CreateOffScreenBitMap
-
- void CreateOffScreenBitMap (Rect *theRect, GrafPtr *offScreen)
- {
- GrafPtr theBWPort;
- BitMap theBitMap;
- long theRowBytes;
-
- theBWPort = (GrafPtr)(NewPtr(sizeof(GrafPort)));
- OpenPort(theBWPort);
- theRowBytes = (long)((theRect->right - theRect->left + 15L) / 16L) * 2L;
- theBitMap.rowBytes = (short)theRowBytes;
- theBitMap.baseAddr = NewPtr((long)theBitMap.rowBytes *
- (theRect->bottom - theRect->top));
- if (theBitMap.baseAddr == 0L)
- RedAlert("\pCouldn't Create Bitmaps");
- theBitMap.bounds = *theRect;
- if (MemError() != noErr)
- RedAlert("\pCouldn't Create Bitmaps");
- SetPortBits(&theBitMap);
- ClipRect(theRect);
- RectRgn(theBWPort->visRgn, theRect);
- EraseRect(theRect);
- *offScreen = theBWPort;
- }
-
- //-------------------------------------------------------------- ZeroRectCorner
-
- void ZeroRectCorner (Rect *theRect) // Offset rect to (0, 0)
- {
- theRect->right -= theRect->left;
- theRect->bottom -= theRect->top;
- theRect->left = 0;
- theRect->top = 0;
- }
-
- //-------------------------------------------------------------- FlashShort
-
- void FlashShort (short theValue)
- {
- GrafPtr wasPort, tempPort;
- Str255 tempStr;
- Rect tempRect;
-
- GetPort(&wasPort);
-
- tempPort = (GrafPtr)NewPtrClear(sizeof(GrafPort));
- OpenPort(tempPort);
-
- NumToString((long)theValue, tempStr);
- MoveTo(20,40);
- SetRect(&tempRect, 18, 20, 122, 42);
- EraseRect(&tempRect);
- DrawString(tempStr);
-
- ClosePort(tempPort);
- SetPort((GrafPtr)wasPort);
- }
-
- //-------------------------------------------------------------- LogNextTick
-
- void LogNextTick (long howMany)
- {
- tickNext = TickCount() + howMany;
- }
-
- //-------------------------------------------------------------- WaitForNextTick
-
- void WaitForNextTick (void)
- {
- do
- {
- }
- while (TickCount() < tickNext);
- }
-
- //-------------------------------------------------------------- TrapExists
-
- Boolean TrapExists (short trapNumber)
- {
- #define kUnimpTrap 0x9F
-
- return ((NGetTrapAddress(trapNumber, ToolTrap) !=
- NGetTrapAddress(kUnimpTrap, ToolTrap)));
- }
-
- //-------------------------------------------------------------- DoWeHaveGestalt
-
- Boolean DoWeHaveGestalt (void)
- {
- #define kGestaltTrap 0xAD
-
- return (TrapExists(kGestaltTrap));
- }
-
- //-------------------------------------------------------------- CenterAlert
-
- void CenterAlert (short alertID)
- {
- AlertTHndl alertHandle;
- Rect theScreen, alertRect;
- short horiOff, vertOff;
- Byte wasState;
-
- theScreen = qd.screenBits.bounds;
- theScreen.top += LMGetMBarHeight();
-
- alertHandle = (AlertTHndl)GetResource('ALRT', alertID);
- if (alertHandle != 0L)
- {
- wasState = HGetState((Handle)alertHandle);
- HLock((Handle)alertHandle);
-
- alertRect = (**alertHandle).boundsRect;
- OffsetRect(&alertRect, -alertRect.left, -alertRect.top);
-
- horiOff = ((theScreen.right - theScreen.left) - alertRect.right) / 2;
- vertOff = ((theScreen.bottom - theScreen.top) - alertRect.bottom) / 3;
-
- OffsetRect(&alertRect, horiOff, vertOff + LMGetMBarHeight());
-
- (**alertHandle).boundsRect = alertRect;
- HSetState((Handle)alertHandle, wasState);
- }
- }
-
- //-------------------------------------------------------------- RectWide
-
- short RectWide (Rect *theRect)
- {
- return (theRect->right - theRect->left);
- }
-
- //-------------------------------------------------------------- RectTall
-
- short RectTall (Rect *theRect)
- {
- return (theRect->bottom - theRect->top);
- }
-
- //-------------------------------------------------------------- CenterRectInRect
-
- void CenterRectInRect (Rect *rectA, Rect *rectB)
- {
- short widthA, tallA;
-
- widthA = RectWide(rectA);
- tallA = RectTall(rectA);
-
- rectA->left = rectB->left + (RectWide(rectB) - widthA) / 2;
- rectA->right = rectA->left + widthA;
-
- rectA->top = rectB->top + (RectTall(rectB) - tallA) / 2;
- rectA->bottom = rectA->top + tallA;
- }
-
- //-------------------------------------------------------------- PasStringCopy
-
- void PasStringCopy (StringPtr p1, StringPtr p2)
- {
- register short stringLength;
-
- stringLength = *p2++ = *p1++;
- while (--stringLength >= 0)
- *p2++ = *p1++;
- }
-
- //-------------------------------------------------------------- CenterDialog
-
- void CenterDialog (short dialogID)
- {
- DialogTHndl dlogHandle;
- Rect theScreen, dlogBounds;
- short hPos, vPos;
- Byte wasState;
-
- theScreen = qd.screenBits.bounds;
- theScreen.top += LMGetMBarHeight();
-
- dlogHandle = (DialogTHndl)GetResource('DLOG', dialogID);
- if (dlogHandle != 0L)
- {
- wasState = HGetState((Handle)dlogHandle);
- HLock((Handle)dlogHandle);
-
- dlogBounds = (**dlogHandle).boundsRect;
- OffsetRect(&dlogBounds, -dlogBounds.left, -dlogBounds.top);
-
- hPos = ((theScreen.right - theScreen.left) - dlogBounds.right) / 2;
- vPos = ((theScreen.bottom - theScreen.top) - dlogBounds.bottom) / 3;
-
- OffsetRect(&dlogBounds, hPos, vPos + LMGetMBarHeight());
-
- (**dlogHandle).boundsRect = dlogBounds;
- HSetState((Handle)dlogHandle, wasState);
- }
- }
-
- //-------------------------------------------------------------- DrawDefaultButton
-
- void DrawDefaultButton (DialogPtr theDialog)
- {
- Rect itemRect;
- Handle itemHandle;
- short itemType;
-
- GetDItem(theDialog, 1, &itemType, &itemHandle, &itemRect);
- InsetRect(&itemRect, -4, -4);
- PenSize(3, 3);
- FrameRoundRect(&itemRect, 16, 16);
- PenNormal();
- }
-
- //-------------------------------------------------------------- PasStringCopyNum
-
- void PasStringCopyNum (StringPtr p1, StringPtr p2, short charsToCopy)
- {
- short i;
-
- if (charsToCopy > *p1) // if trying to copy more chars than there are
- charsToCopy = *p1; // reduce the number of chars to copy to this size
-
- *p2 = charsToCopy;
-
- *p2++;
- *p1++;
-
- for (i = 0; i < charsToCopy; i++)
- *p2++ = *p1++;
- }
-
- //-------------------------------------------------------------- GetDialogString
-
- void GetDialogString (DialogPtr theDialog, short item, StringPtr theString)
- {
- Rect itemRect;
- Handle itemHandle;
- short itemType;
-
- GetDItem(theDialog, item, &itemType, &itemHandle, &itemRect);
- GetIText(itemHandle, theString);
- }
-
- //-------------------------------------------------------------- SetDialogString
-
- void SetDialogString (DialogPtr theDialog, short item, StringPtr theString)
- {
- Rect itemRect;
- Handle itemHandle;
- short itemType;
-
- GetDItem(theDialog, item, &itemType, &itemHandle, &itemRect);
- SetIText(itemHandle, theString);
- }
-
- //-------------------------------------------------------------- SetDialogNumToStr
-
- void SetDialogNumToStr (DialogPtr theDialog, short item, long theNumber)
- {
- Str255 theString;
- Rect itemRect;
- Handle itemHandle;
- short itemType;
-
- NumToString(theNumber, theString);
- GetDItem(theDialog, item, &itemType, &itemHandle, &itemRect);
- SetIText(itemHandle, theString);
- }
-
- //-------------------------------------------------------------- GetDialogNumFromStr
-
- void GetDialogNumFromStr (DialogPtr theDialog, short item, long *theNumber)
- {
- Str255 theString;
- Rect itemRect;
- Handle itemHandle;
- short itemType;
-
- GetDItem(theDialog, item, &itemType, &itemHandle, &itemRect);
- GetIText(itemHandle, theString);
- StringToNum(theString, theNumber);
- }
-
- //-------------------------------------------------------------- DisableControl
-
- void DisableControl (DialogPtr theDialog, short whichItem)
- {
- Rect iRect;
- Handle iHandle;
- short iType;
-
- GetDItem(theDialog, whichItem, &iType, &iHandle, &iRect);
- HiliteControl((ControlHandle)iHandle, kInactive);
- }
-
-